from Queen.datasource.base_datasource import BaseDatasource
from logger_config import logger_factory
import pandas as pd
import cx_oracle

logger = logger_factory()


class SQLDatasource(BaseDatasource):
    def __init__(self, sql):
        super().__init__()
        self.sql = sql

    def read_data(self):
        try:
            connection = cx_oracle..connect(user='B2CIM', password='B2CIM1234', dsn='10.236.12.152:1521/B2MFGDB')
            connection.CallTimeout = 60*1000
            data = pd.read_sql(self.sql, connection)
            logger.info(f'read sql {self.sql}')
        except Exception as e:
            logger.error(f'read sql {self.sql} fail, please check it')
            logger.error(e)
            return None
        return data
